package com.zhixi.pattern.notimportant.combinationpattern;

import com.zhixi.pattern.notimportant.combinationpattern.operation.Branch;
import com.zhixi.pattern.notimportant.combinationpattern.operation.Corp;
import com.zhixi.pattern.notimportant.combinationpattern.operation.Leaf;

import java.util.ArrayList;

/**
 * 组合模式
 *
 * @作者 ShuHaoNan
 * @邮箱 shuhaonan1@gmail.com
 * @时间 2020/11/17 17:25
 */
public class CombinationPattern {

    public static void main(String[] args) {
        Branch branch = new Branch("小米", "总经理", 100000);
        Leaf leaf1 = new Leaf("m1", "开发人员", 10000);
        Leaf leaf2 = new Leaf("m2", "开发人员", 10000);
        Leaf leaf3 = new Leaf("m3", "开发人员", 10000);
        Leaf leaf4 = new Leaf("m4", "开发人员", 10000);
        Leaf leaf5 = new Leaf("m5", "开发人员", 10000);
        Leaf leaf6 = new Leaf("m6", "开发人员", 10000);
        Leaf leaf7 = new Leaf("m7", "开发人员", 10000);
        Leaf leaf8 = new Leaf("m8", "开发人员", 10000);
        Branch branch1 = new Branch("小白", "开发一组组长", 20000);
        Branch branch2 = new Branch("小黑", "开发二组组长", 20000);
        branch.addSubordinate(branch1);
        branch.addSubordinate(branch2);
        branch1.addSubordinate(leaf1);
        branch1.addSubordinate(leaf2);
        branch1.addSubordinate(leaf3);
        branch1.addSubordinate(leaf4);
        branch2.addSubordinate(leaf5);
        branch2.addSubordinate(leaf6);
        branch2.addSubordinate(leaf7);
        branch2.addSubordinate(leaf8);
        getTreeInfo(branch);
    }

    private static String getTreeInfo(Branch root) {
        ArrayList<Corp> subordinate = root.getSubordinate();
        StringBuilder info = new StringBuilder();
        for (Corp corp : subordinate) {
            if (corp instanceof Leaf) {
                info.delete(0, info.length()); /* 清空缓冲区 */
                info.append(corp);
                System.out.println(info);
            } else {
                info.delete(0, info.length()); /* 清空缓冲区 */
                info.append(corp);
                info.append(getTreeInfo((Branch) corp));
                System.out.println(info);
            }
        }
        return new String(info);
    }

}
